package thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class test18 {
    public static void main(String[] args) {
        BlockingDeque<Integer> queue = new LinkedBlockingDeque<>(100);
        // 生产者模式
        Thread t1 = new Thread(() -> {
            int count = 1;
            while (true) {
                try {
                    queue.put(count);
                    System.out.println(count);
                    count++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                /*try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }*/
            }
        });
        // 消费者模式
        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    int count = queue.take();
                    System.out.println(count);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
